package com.example.demo.model.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Data;
import org.hibernate.validator.constraints.URL;

import javax.validation.constraints.*;
import java.time.LocalDate;
import java.util.List;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL) // 序列化时忽略null字段
@JsonIgnoreProperties(ignoreUnknown = true)
public class PaperSubmissionDTO {
    @NotBlank(message = "论文标题不能为空")
    @Size(max = 500, message = "论文标题长度不能超过500字符")
    @JsonProperty("paperTitle") // 显式指定JSON字段名
    @JsonDeserialize(using = TrimStringDeserializer.class) // 自定义处理
    private String paperTitle;

    @NotBlank(message = "期刊名称不能为空")
    @Size(max = 200, message = "期刊名称长度不能超过200字符")
    @JsonProperty("journal")
    @JsonDeserialize(using = TrimStringDeserializer.class)
    private String journal;

    @Size(max = 50, message = "期号长度不能超过50字符")
    @JsonProperty("issue")
    @JsonDeserialize(using = TrimStringDeserializer.class)
    private String issue;

    @Size(max = 50, message = "卷号长度不能超过50字符")
    @JsonProperty("volume")
    @JsonDeserialize(using = TrimStringDeserializer.class)
    private String volume;

    @Size(max = 100, message = "页码范围长度不能超过100字符")
    @JsonProperty("pageRange")
    @JsonDeserialize(using = TrimStringDeserializer.class)
    private String pageRange;

    @Size(max = 50, message = "索引级别长度不能超过50字符")
    @JsonProperty("indexLevel")
    @JsonDeserialize(using = TrimStringDeserializer.class)
    private String indexLevel;

    @NotNull(message = "作者列表不能为空")
    @Size(min = 1, message = "至少需要一名作者")
    @JsonProperty("authors")
    private List<AuthorInfo> authors;

    @NotBlank(message = "第一作者不能为空")
    @Size(max = 100, message = "第一作者名称长度不能超过100字符")
    @JsonProperty("firstAuthor")
    @JsonDeserialize(using = TrimStringDeserializer.class)
    private String firstAuthor;

    @Size(max = 100, message = "通讯作者名称长度不能超过100字符")
    @JsonProperty("correspondingAuthor")
    @JsonDeserialize(using = TrimStringDeserializer.class)
    private String correspondingAuthor;

    @NotNull(message = "发表日期不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd")
    @PastOrPresent(message = "发表日期不能是未来日期")
    @JsonProperty("publishDate")
    private LocalDate publishDate;

    @DecimalMin(value = "0.0", message = "影响因子不能小于0")
    @JsonProperty("impactFactor")
    private Double impactFactor;

    @Size(max = 500, message = "论文链接长度不能超过500字符")
    @URL(message = "论文链接格式不正确")
    @JsonProperty("paperLink")
    @JsonDeserialize(using = TrimStringDeserializer.class)
    private String paperLink;

    // 必须有无参构造函数
    public PaperSubmissionDTO() {}

    @Data
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public static class AuthorInfo {
        @NotBlank(message = "作者姓名不能为空")
        @Size(max = 100, message = "作者姓名长度不能超过100字符")
        @JsonProperty("name")
        @JsonDeserialize(using = TrimStringDeserializer.class)
        private String name;

        @Size(max = 200, message = "作者单位长度不能超过200字符")
        @JsonProperty("affiliation")
        @JsonDeserialize(using = TrimStringDeserializer.class)
        private String affiliation;

        @NotNull(message = "作者顺序不能为空")
        @Min(value = 1, message = "作者顺序最小为1")
        @JsonProperty("order")
        private Integer order;
    }

    // 添加更严格的setter方法
    public void setPaperTitle(String paperTitle) {
        this.paperTitle = (paperTitle == null) ? null : paperTitle.trim();
        if (this.paperTitle != null && this.paperTitle.isEmpty()) {
            this.paperTitle = null;
        }
    }

}